package com.atguigu.eduservice.controller.front;

import com.atguigu.common_uitls.JwtUtils;
import com.atguigu.common_uitls.R;
import com.atguigu.common_uitls.orderVo.CourseOrderVo;
import com.atguigu.eduservice.client.OrderClient;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.frontVo.CourseWebVo;
import com.atguigu.eduservice.entity.frontVo.courseFrontVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * zz: wzh
 * time 2022 10 05 17 06
 **/
@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {
    @Autowired
    private EduCourseService eduCourseService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private OrderClient orderClient;

    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                @RequestBody(required = false)courseFrontVo courseFrontVo){

        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map= eduCourseService.getCourseFrontList(pageCourse,courseFrontVo);
        return R.ok().data(map);
    }
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        CourseWebVo courseWebVo=eduCourseService.getFrontCourseInfo(courseId);
        List<ChapterVo> chapterVideList = eduChapterService.getChapterVideoByCourseId(courseId);
        //根据课程id,和用户id查询看用户是否购买了该课程
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean IsBuy = orderClient.isBuyCourse(courseId, memberId);

        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideList",chapterVideList).data("isBuy",IsBuy);
    }
    @PostMapping("getCourseOrderInfo/{id}")
    public CourseOrderVo getCourseOrderInfo(@PathVariable String id){
        CourseWebVo frontCourseInfo = eduCourseService.getFrontCourseInfo(id);
        CourseOrderVo courseOrderVo=new CourseOrderVo();
        BeanUtils.copyProperties(frontCourseInfo,courseOrderVo);
        return courseOrderVo;
    }


}
